/**
 * @file    drv_layer.c
 * @brief   MCU驱动层
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-20
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-20 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "drv_layer.h"
#include "drv_clk.h"
#include "drv_gpio.h"
#include "drv_wdg.h"
#include "drv_uart.h"
#include "drv_can.h"
#include "drv_mcu.h"
#include "drv_gpt.h"
#include "drv_rtc.h"
#include "drv_spi.h"
#include "drv_adc.h"
#include "boot_param.h"
#include "iap_master.h"
#include "cfg_prj.h"
#include "iap_boot.h"
#include "if_di.h"
/* macro/define --------------------------------------------------------------*/
#define POW_UP_DLY          (500)
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
static void check_power_up(void);
/* static variables ----------------------------------------------------------*/
void drv_gpt_test(uint32_t time);
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief
 * @param
 * @return
*/
void drv_layer_init(void)
{
    /* 配置系统时钟频率 */
	drv_clk_set_sys(SYS_FREQ_MID);
    
	drv_wdg_init(WDT_TIMEOUT_TIME);
    
    boot_par_init();
    
    drv_gpio_cfg_mode(SW_PORT       , SW_PIN       , GPIO_MODE_INPUT);
    drv_gpio_cfg_mode(CHG_SIGN_PORT , CHG_SIGN_PIN , GPIO_MODE_INPUT);
    drv_gpio_cfg_mode(SYS_POW_PORT  , SYS_POW_PIN  , GPIO_MODE_OUTPUT);
    drv_gpio_set_pin(SYS_POW_PORT , SYS_POW_PIN);
    
	If_di_init_all();
	
    check_power_up();
#ifdef USE_MASTER_UPGARDE_SLAVE      
    if(TRUE == boot_param_is_upgrade())
	{
		calc_val = app_calc_app_crc(0xFFFFFFFF , APP_START_ADDR , boot_param_read_par(BP_ID_APP_SIZE) , 1);
		read_val = boot_param_read_par(BP_ID_APP_CRC);
		if(calc_val == read_val)//if(boot_param_read_par(BP_ID_APP_CRC) == app_calc_app_crc(0xFFFFFFFF , APP_START_ADDR , boot_param_read_par(BP_ID_APP_SIZE) , 1))
		{
			iap_master_start_upgrade_slave();
		}
	}
#endif
	
    boot_clear_upgrade_flag();
    
	drv_spi_init(DRV_SPI2);
	
	drv_adc_init();

	boot_par_init();
	/* 初始化CAN驱动 */
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
static void check_power_up(void)
{
	drv_wdg_feed();
        
    if(drv_gpio_read_pin(SW_PORT , SW_PIN) == GPIO_SET)
    {
        drv_gpio_reset_pin(DCDC_EN_PORT , DCDC_EN_PIN);
        mcu_delay_ms(10);
        drv_gpio_reset_pin(SYS_POW_PORT , SYS_POW_PIN);
		drv_wdg_feed();
        mcu_delay_ms(500);
    }
}
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


